<template>
  <a-modal
    :title="$t('验票设置')"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    :okText="$t('保存')"
    @cancel="handleCancel"
    @ok="handleOk"
    class="customHeight customHeightChild modal_title"
    v-drag
  >
    <template slot="footer">
      <a-button @click="handleCancel" :loading="confirmLoading">{{ $t('关闭') }}</a-button>
    </template>
    <a-form-model ref="form" :model="model" :rules="validatorRules">
      <a-row :gutter="24" align="middle" type="flex">
        <a-col :span="24"
          ><div style="color: #000; font-weight: 600">{{ $t('有效期设置') }}</div></a-col
        >
        <a-col :span="24" style="margin: 10px 0; padding-left: 60px">
          <div style="color: orange">
            {{ $t('说明：超过有效天数的彩票不可进行验票/存票业务；设置为0表示不限制。') }}
          </div>
        </a-col>
        <a-col :span="24">
          <a-form-model-item :label="$t('有效天数')" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <a-input v-model="model.ivalue" style="width: 300px"></a-input>
            <a-button v-has="'BisMemberLotteryController:edit'" type="primary" @click="handleOk" :loading="confirmLoading" style="margin-left: 10px">{{
              $t('保存')
            }}</a-button>
          </a-form-model-item>
        </a-col>
      </a-row>
    </a-form-model>
    <a-form-model ref="tform" :model="tmodel" :rules="tvalidatorRules">
      <a-row :gutter="24" align="middle" type="flex">
        <a-col :span="24"
          ><div style="color: #000; font-weight: 600">{{ $t('兑换商品设置') }}</div></a-col
        >
        <a-col :span="24" style="margin: 10px 0; padding-left: 60px">
          <div style="color: orange">
            {{ $t('说明：兑换礼品时若数字彩票面额超过礼品价值则剩余彩票可根据运营需要选择清零或找零。') }}
          </div>
        </a-col>
        <a-col :span="24">
          <a-form-model-item :label="$t('剩余彩票')" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <a-radio-group v-model="tmodel.ivalue" style="margin-left: 5px">
              <a-radio value="0">{{ $t('清零') }}</a-radio>
              <a-radio value="1">{{ $t('找零') }}</a-radio>
            </a-radio-group>
            <a-button v-has="'BisMemberLotteryController:edit'" type="primary" @click="thandleOk" :loading="confirmLoading" style="margin-left: 10px">{{
              $t('保存')
            }}</a-button>
          </a-form-model-item>
        </a-col>
      </a-row>
    </a-form-model>
  </a-modal>
</template>
<script>
import { getAction, httpAction } from '@/api/manage'
export default {
  name: 'CheckLotteryModal',
  data() {
    return {
      width: 800,
      visible: false,
      disableSubmit: false,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 4 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 18 },
      },
      confirmLoading: false,
      model: {
        name: 'lotteryValid',
        ivalue: 0,
      },
      validatorRules: {},
      tmodel: {
        name: 'remainLottery',
        ivalue: '0',
      },
      tvalidatorRules: {},
    }
  },
  methods: {
    show() {
      let that = this
      that.confirmLoading = false
      that.getEquip()
      that.visible = true
    },
    getEquip() {
      let that = this
      getAction('/bis/bisSysParameter/list', { name: 'lotteryValid' }).then((res) => {
        if (res.success) {
          if (res.result.records.length > 0) {
            that.model = Object.assign({}, res.result.records[0])
          } else {
            that.model = {
              name: 'lotteryValid',
              ivalue: 0,
            }
          }
        } else {
          that.$message.error(res.message)
        }
      })
      getAction('/bis/bisSysParameter/list', { name: 'remainLottery' }).then((res) => {
        if (res.success) {
          if (res.result.records.length > 0) {
            that.tmodel = Object.assign({}, res.result.records[0])
            that.tmodel.ivalue = '' + that.tmodel.ivalue
          } else {
            that.tmodel = {
              name: 'remainLottery',
              ivalue: '0',
            }
          }
        } else {
          that.$message.error(res.message)
        }
      })
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    handleCancel() {
      this.close()
    },
    handleOk() {
      let that = this
      that.confirmLoading = true
      let url = '', method = ''
      if (that.model.id) {
        url = '/bis/bisSysParameter/edit'
        method = 'put'
      } else {
        url = '/bis/bisSysParameter/add'
        method = 'post'
      }
      if (!that.model.ivalue) {
        that.model.ivalue = 0
      }
      httpAction(url, that.model, method).then((res) => {
        if (res.success) {
          that.$message.success(res.message)
        } else {
          that.$message.error(res.message)
        }
      }).finally(()=>{
        that.confirmLoading = false
      })
    },
    thandleOk() {
      let that = this
      that.confirmLoading = true
      let url = '', method = ''
      if (that.tmodel.id) {
        url = '/bis/bisSysParameter/edit'
        method = 'put'
      } else {
        url = '/bis/bisSysParameter/add'
        method = 'post'
      }
      if (!that.tmodel.ivalue) {
        that.tmodel.ivalue = '0'
      }
      httpAction(url, that.tmodel, method).then((res) => {
        if (res.success) {
          that.$message.success(res.message)
        } else {
          that.$message.error(res.message)
        }
      }).finally(()=>{
        that.confirmLoading = false
      })
    },
  },
}
</script>

<style>
</style>